var fileUploadOptions = {};

function formatNum(Num1,Num2){ 
     if(isNaN(Num1)||isNaN(Num2)){ 
           return(0); 
     }else{ 
           Num1=Num1.toString(); 
           Num2=parseInt(Num2); 
           if(Num1.indexOf('.')==-1){ 
                 return(Num1); 
           }else{ 
                 var b=Num1.substring(0,Num1.indexOf('.')+Num2+1); 
                 var c=Num1.substring(Num1.indexOf('.')+Num2+1,Num1.indexOf('.')+Num2+2); 
                 if(c==""){ 
                       return(b); 
                 }else{ 
                       if(parseInt(c)<5){ 
                             return(b); 
                       }else{ 
                             return((Math.round(parseFloat(b)*Math.pow(10,Num2))+Math.round(parseFloat(Math.pow(0.1,Num2).toString().substring(0,Math.pow(0.1,Num2).toString().indexOf('.')+Num2+1))*Math.pow(10,Num2)))/Math.pow(10,Num2)); 
                       } 
                 } 
           } 
     } 
};

/**
 * 文件上传按钮
 */
function fn_fileUploadPost(){
    try {
    	loadmask();
		/* */
        $("#fileUploadForm").ajaxSubmit({
			dataType: 'json',
			type:'post',
			url:ctx + "/sm/File!upload.action",
            success: function(data, status, xhr, $form){
                unmask();
                if (data.success) {
                    fileUploadOptions.success(data.result);
                }else{
                    $.messager.show({
                          msg: data.msg
                    });
                }
                showHideFun("fileWin","close");
            }
        });
    }catch (e) {
        unmask();
		showHideFun("fileWin","close");
        $.messager.show({
            msg: e.message
        });
    }
    
}

/**
 * 文件上传菜单
 * @param {Object} businessObject 业务对象名
 * @param {Object} targetTableSid 业务数据SID(OPTIONAL)
 * @param {Object} types 附件类型
 * @param {Object} isTempFile 是否临时文件
 * @param {Object} url 上传的URL
 * @param {Object} allowedFileTypes  允许的文件内容,以|分隔 gif|jpg
 * @param {Object} allowedSingleFileSize 允许上传的个文件大小
 * @param {Object} callback 回调function
 * @param {Object} maxLength 允许上传的文件数
 */
function fn_fileupload(businessObject, targetTableSid, types, isTempFile, allowedFileTypes, allowedSingleFileSize, maxLength, callback){
	try{
		//function  fn_fileUpload(){
    $("#fileWin").html("<form method=\"post\" id=\"fileUploadForm\">");
    $("#fileUploadForm").append("<table class=\"clsTblWithBorder\" id=\"id_table\">");
    $("#id_table").append("<tr id=\"id_tr1\"></tr><tr id=\"id_tr2\"></tr>");
    $("#id_tr1").append(" <td height=\"30\" id=\"id_td1\">");
    $("#id_td1").append("<input type=\"file\" class=\"easyui-validatebox\" name=\"attachment\" id=\"attachment\" size=\"40\" />");
    $("#id_td1").append("<input type=\"hidden\" name=\"targetTableName\" id=\"targetTableName\" value=\"" + businessObject + "\"/>");
    $("#id_td1").append("<input type=\"hidden\" name=\"targetTableSid\" id=\"targetTableSid\" value=\"" + targetTableSid + "\"/>");
    $("#id_td1").append("<input type=\"hidden\" name=\"types\" id=\"types\" value=\"" + types + "\"/>");
    $("#id_td1").append("<input type=\"hidden\" name=\"isTempFile\" id=\"isTempFile\" value=\"" + isTempFile + "\"/>");
	$("#id_tdtd").append("<input type=\"hidden\" name=\"allowedSingleFileSize\" id=\"allowedSingleFileSize\" value=\"" + allowedSingleFileSize + "\"/>");
    $("#id_tr2").append("<td height=\"20\" id=\"id_td2\">");
    $("#id_td2").append("可上传文件类型：<span id=\"acceptFileExtList\"></span><br/>最大文件大小：<span id=\"acceptFileSizeLimit\"></span><br/>");
	$("#id_td2").append("可上传文件数量：<span id=\"acceptFileLengthList\"></span>");
    $("#fileWin").append("<div align=\"center\" border=\"false\" id=\"id_div1\">");
    $("#id_div1").append("<a href=\"#\" class=\"easyui-linkbutton\" onclick=\"fn_fileUploadPost()\">上传</a>&nbsp;&nbsp;");
    $("#id_div1").append("<a href=\"#\" class=\"easyui-linkbutton\" onclick=\"showHideFun('fileWin','close');\">取消</a>");
    $.parser.parse($("#fileWin"));
    $("#fileWin").window({
        title: '上传',
        collapsible: false,
        minimizable: false,
        maximizable: false
    });
    // 初始化文件上传控件-BEGIN
    fileUploadOptions = {
        sizeLimit: allowedSingleFileSize,
        accept: allowedFileTypes,
        maxLength: maxLength,
        success: callback
    };
    $('#attachment').MultiFile({
        max: fileUploadOptions.maxLength,
        accept: fileUploadOptions.accept,
		sizeLimit:fileUploadOptions.sizeLimit
    });
    $("#acceptFileExtList").html(fileUploadOptions.accept);
    $("#acceptFileSizeLimit").html(formatNum(allowedSingleFileSize/1024/1024,3) + "M" );
    $("#acceptFileLengthList").html(maxLength);
    showHideFun("fileWin","open");
	}catch(e){
		showHideFun("fileWin","close");
		$.messager.show({
            msg: e.message
        });
	}
    
}